package app.android.kdictionary.Component;

import android.app.PendingIntent;
import android.app.PendingIntent.CanceledException;
import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Toast;
import app.android.kdictionary.R;
import app.android.kdictionary.R.id;
import app.android.kdictionary.Activity.DetailActivity;
import app.android.kdictionary.Activity.HeadwordActivity;
import app.android.kdictionary.Activity.OtherSelectActivity;
import app.android.kdictionary.Activity.PickerActivity;
import app.android.kdictionary.Activity.SetupActivity;

public class Bottom extends LinearLayout {
	private Context mContext;
	private ImageButton menu1Button;
	private ImageButton menu2Button;
	private ImageButton menu3Button;
	private ImageButton menuSetButton;
	
	public Bottom(Context context) {
		this(context, null);
	}
	
	public Bottom(Context context, AttributeSet attrs) {
		super(context, attrs);
		initLayout(context, attrs);
		findView();
		setListener();
	}

	
	private void initLayout(Context context, AttributeSet attrs) {
		mContext = context;
		
		String infService = Context.LAYOUT_INFLATER_SERVICE;
		LayoutInflater li = (LayoutInflater)getContext().getSystemService(infService);
		li.inflate(R.layout.c_bottom, this, true);
	}
	
	private void findView() {
		menu1Button = (ImageButton)findViewById(id.ImageButton_01);
		menu2Button = (ImageButton)findViewById(id.ImageButton_02);
		menu3Button = (ImageButton)findViewById(id.ImageButton_03);
		menuSetButton = (ImageButton)findViewById(id.ImageButton_setting);
	}
	
	private void setListener() {
		menu1Button.setOnClickListener(mHeadwordClickListener);
		menu2Button.setOnClickListener(mDetailSearchListener);
		menu3Button.setOnClickListener(mOtherClickListener);
		menuSetButton.setOnClickListener(mSetupClickListener);
	}

    private OnClickListener mHeadwordClickListener = new OnClickListener() {
    	
        public void onClick(View v) {

        	if(mContext.getClass().getName().contains("HeadwordActivity")) {

        		Toast.makeText(getContext(), "현재 화면입니다.", Toast.LENGTH_SHORT).show();
        		
        	} else {
        	
	        	Intent startIntent  = new Intent(mContext, HeadwordActivity.class);
	        	startIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
	        	PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, startIntent, 0);
				try {
					pendingIntent.send();
				} catch( CanceledException e ) {
					e.printStackTrace();
				}
	//			((Activity) mContext).finish();
				
				Intent i = new Intent();
				i.setAction("finish");
				mContext.sendBroadcast(i);
        	}
        }
    };
    
    private OnClickListener mDetailSearchListener = new OnClickListener() {
    	
    	public void onClick(View v) {

    		if(mContext.getClass().getName().contains("DetailActivity")) {

        		Toast.makeText(getContext(), "현재 화면입니다.", Toast.LENGTH_SHORT).show();
    			
    		} else {
	    		Intent startIntent  = new Intent(mContext, DetailActivity.class);
	    		startIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, startIntent, 0);
				try {
					pendingIntent.send();
				} catch( CanceledException e ) {
					e.printStackTrace();
				}
	//			((Activity) mContext).finish();
				
				Intent i = new Intent();
				i.setAction("finish");
				mContext.sendBroadcast(i);
    		}
    	}
    };
    
    
    private OnClickListener mOtherClickListener = new OnClickListener() {

    	public void onClick(View v) {

    		if(mContext.getClass().getName().contains("OtherSelectActivity")) {

        		Toast.makeText(getContext(), "현재 화면입니다.", Toast.LENGTH_SHORT).show();
    			
    		} else {

    			if( mContext.getClass().equals(PickerActivity.class) ) {

    			}
        	
	        	Intent startIntent  = new Intent(mContext, OtherSelectActivity.class);
	        	startIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
	        	PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, startIntent, 0);
				try {
					pendingIntent.send();
				} catch( CanceledException e ) {
					e.printStackTrace();
				}
	//			((Activity) mContext).finish();
				
				Intent i = new Intent();
				i.setAction("finish");
				mContext.sendBroadcast(i);
    		}
        }
    };
    
    private OnClickListener mSetupClickListener = new OnClickListener() {
        public void onClick(View v) {

        	Intent startIntent  = new Intent(mContext, SetupActivity.class);
        	startIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        	PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, startIntent, 0);
			try {
				pendingIntent.send();
			} catch( CanceledException e ) {
				e.printStackTrace();
			}
//			((Activity) mContext).finish();
			
			Intent i = new Intent();
			i.setAction("finish");
			mContext.sendBroadcast(i);
        }
    };
}